package com.xiezc;

import com.alibaba.fastjson.JSONObject;
import com.xiaoleilu.hutool.cache.impl.TimedCache;
import com.xiezc.action.DownloadAction;
import com.xiezc.action.FileInfoAction;
import com.xiezc.action.LoginAction;
import com.xiezc.action.UploadAction;
import com.xiezc.dto.RespData;

import com.xiezc.entity.FileInfo;
import com.xiezc.entity.User;
import com.xiezc.service.MainService;
import com.xiezc.service.UpFileRunnable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by wb-xzc291800 on 2017/6/22.
 */
@SpringBootApplication
public class TBpanClientApplication implements CommandLineRunner {

    @Resource
    private MainService mainService;


    @Resource
    private UpFileRunnable upFileRunnable;
    @Resource
    private LoginAction loginAction;

    /**
     * 缓存工具类
     *
     * @return
     */
    @Bean
    public TimedCache createTimedCache() {
        TimedCache timedCache = new TimedCache(12 * 60 * 60 * 1000L);
        return timedCache;
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TBpanClientApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        //获得用户
        User user = mainService.getUser();
        loginAction.register(user);
        //登录
        mainService.login(user);
        //确定同步文件夹
        String pathSet = user.getPathSet();
        boolean flag = mainService.configFilePath(pathSet);
        //获得本地文件夹文件的路径
        Path path = Paths.get(pathSet);
        List<Path> list = mainService.listFiles(path);
        //获得本地文件的信息
        Stream<FileInfo> fileInfoStream = mainService.findAllFileInfo(list, path);

        //服务器上有本地没有的文件
        Stream<FileInfo> serverFileInfo = mainService.diffFile(fileInfoStream, 0);
        if (serverFileInfo.count() > 0) {
            if (flag) {
                serverFileInfo.forEach(lf -> {
                    mainService.download(lf);
                });
            } else {

                List<FileInfo> fileInfos = serverFileInfo.collect(Collectors.toList());
                mainService.deleteFiles(fileInfos);
            }
        }
        //本地有服务器上没有的文件
        Stream<FileInfo> localFileInfo = mainService.diffFile(fileInfoStream, 1);
        List<FileInfo> localFileInfoList = localFileInfo.collect(Collectors.toList());
        mainService.uploadFileInfos(localFileInfoList);

        upFileRunnable.setFileInfos(localFileInfoList);
        Thread thread = new Thread(upFileRunnable);
        thread.start();

    }


}
